window.alert = function(str) {
    var alertFram = document.createElement("DIV");
    alertFram.id = "alertFram";
    alertFram.style.position = "fixed";
    alertFram.style.width = "100%";
    alertFram.style.height = "100%";
    alertFram.style.top = "0";
    alertFram.style.backgroundColor = "rgba(0,0,0,.3)";
    alertFram.style.zIndex = "300";
    strHtml = "<ul style=\"list-style:none;margin:0px;padding:0px;width:8.83rem;height:6.92rem;margin:3.01rem auto;background:#fff;border-radius:3%;position:relative\">\n";
    strHtml += " <li style=\"padding-top:2rem;display:block;\"><img src=\"../img/tanchuang.png\" style=\"width:1.73rem;height:1.73rem;margin:0 auto;display:block\"></li>\n";
    strHtml += " <li style=\"background:#fff;text-align:center;font-size:0.6rem;height:1.27rem;padding-top:0.67rem;\">" + str + "</li>\n";
    strHtml += " <a href=\"javascript:void(0)\" onclick=\"doOk()\" type=\"button\" style=\"position:absolute;top:10px;margin-left:8rem;height:0.48rem;width:0.48rem;display:block;\"><img src=\"../img/cuowu.png\" style=\"display:block;height:0.48rem;width:0.48rem;\"></a>\n";
    strHtml += "</ul>\n";
    alertFram.innerHTML = strHtml;
    document.body.appendChild(alertFram);
    this.doOk = function() {
        alertFram.style.display = "none";
    }
    alertFram.focus();
    document.body.onselectstart = function() {
        return false;
    };
}


var register = {
    manageId: 2,
    userType: 2,
    isEdit: false,
    taskId: 1,

    init: function() {
        var flag = false;
        if (GetQueryString("userId")) {
            this.manageId = GetQueryString("userId"); //获取当前会话的id
        }
        if (GetQueryString("type")) {
            this.userType = GetQueryString("type"); //获取当前的用户类型
        }
        if (this.userType == 3) {
            flag = true; //如果为true的时候 userType 值为3的时候 显示公司列表
        }

        this.initData(flag); //初始化数据
        this.sendCode();
        this.bindEvent(flag);
    },
    init2: function() {
        if (GetQueryString("userId")) {
            this.manageId = GetQueryString("userId"); //获取当前会话的id
        }

        if (localStorage.getItem("isEdit")) {
            this.isEdit = true; //是否是修改的情况
        }

        if (this.isEdit) {
            if (GetQueryString("type")) {
                this.userType = GetQueryString("type"); //获取当前的用户类型
            }
            if (localStorage.getItem("data_taskId")) {
                this.taskId = localStorage.getItem("data_taskId");
            }
        }
        this.initData2();
        this.bindEvent2();
    },
    //发布任务初始化数据
    initData2: function() {
        var thiz = this;
        $('#Start_time').date({
            theme: "datetime"
        });

        $('#End_time').date({
            theme: "datetime"
        });

        //获取公司列表
        start.postAjaxReq({
            param: {
                "manage_id": thiz.manageId,
            },
            type: "GET",
            url: "/worker/CompanyId",
            success: function(data) {
                var obj = data.result.data,
                    str = '';
                for (var i = 0; i < obj.length; i++) {
                    str += '<option value="' + obj[i].c_id + '" class="drop">' + obj[i].c_name + '</option>';
                }
                $("#Elect").append(str);

            }
        });

        $("#Elect").change(function(value) {
            //获取管理员列表
            start.postAjaxReq({
                param: {
                    "company_id": value.target.value,
                },
                type: "GET",
                url: "/worker/enterpriseList",
                success: function(data) {
                    var obj = data.result.data;
                    var name = '';
                    for (var i = 0; i < obj.length; i++) {
                        name += '<option value="' + obj[i].w_id + '" class="drops">' + obj[i].w_name + '</option>';
                    }
                    $("#manag").append(name);
                }
            });
        });


        //如果是从编辑页面过来的话 执行这个
        if (this.isEdit) {
            start.postAjaxReq({
                param: {
                    "id": thiz.manageId,
                    "task_id": thiz.taskId,
                    "type": thiz.userType,
                    "status": 5
                },
                type: "GET",
                url: "/task/getTaskInfo",
                success: function(data) {
                    var obj = data.result;
                    $("#Elect").find("option[value='" + obj.company_id + "']").attr("selected", true);
                    start.postAjaxReq({
                        param: {
                            "company_id": obj.company_id,
                        },
                        type: "GET",
                        url: "/worker/enterpriseList",
                        success: function(data) {
                            var obj2 = data.result.data;
                            var name = '';
                            for (var i = 0; i < obj2.length; i++) {
                                name += '<option value="' + obj2[i].w_id + '" class="drops">' + obj2[i].w_name + '</option>';
                            }
                            $("#manag").append(name);
                            $("#manag").find("option[value='" + obj.enterprise_id + "']").attr("selected", true);
                        }
                    });

                    $("#Start_time").val(obj.start_time.substr(0, 16));
                    $("#End_time").val(obj.end_time.substr(0, 16));
                    $("#Price").val(obj.price);
                    $("#Dinner_time").val(obj.dinner_time);
                    $("#Number").val(obj.number);
                    $("#Contacts").val(obj.contacts);
                    $("#Workplace").val(obj.workplace);
                    $("#Car_subsidy").val(obj.car_subsidy);
                    $("#Other_subsidy").val(obj.other_subsidy);
                    $("#Phone").val(obj.phone);
                    $("#workplace").val(obj.workplace);
                }
            })
        }
    },
    //发布任务用到的判断条件
    _isLegal2: function(obj) {
        if (obj.company_id == 'text') {
            alert('请选择公司!');
            return false;
        }
        if (obj.enterprise_id == "text") {
            alert('请选择管理人员!');
            return false;
        }
        if (!$.trim(obj.start_time)) {
            alert("请选择开始时间!");
            return false;
        }
        if (!$.trim(obj.end_time)) {
            alert("请选择结束时间!");
            return false;
        }
        if (!$.trim(obj.price) || isNaN(obj.price)) {
            alert("请填写正确时薪!");
            return false;
        }
        if (!$.trim(obj.dinner_time) || isNaN(obj.dinner_time)) {
            alert("请填写正确用餐时间!");
            return false;
        }
        if (!$.trim(obj.car_subsidy) || isNaN(obj.car_subsidy)) {
            alert("请填写正确的车费补贴!");
            return false;
        }
        if (!$.trim(obj.other_subsidy) || isNaN(obj.other_subsidy)) {
            alert("请填写正确的其他补贴!");
            return false;
        }
        if (!$.trim(obj.number) || isNaN(obj.number)) {
            alert("请填写正确的预计招聘人数!");
            return false;
        }
        if (!$.trim(obj.contacts)) {
            alert("请填写联系人!");
            return false;
        }
        if (!$.trim(obj.phone) || isNaN(obj.phone)) {
            alert("请填写正确的手机号码!");
            return false;
        }
        if (!$.trim(obj.workplace)) {
            alert("请填写详细地点!");
            return false;
        }
        return true;
    },

    //发布任务用到的提交事件
    bindEvent2: function() {
        var thiz = this,
            url = "";
        $("#submit").click(function() {
            var obj = {
                "company_id": $('select[name="Elect"]').val(),
                "dinner_time": $("#Dinner_time").val(),
                "start_time": $("#Start_time").val(),
                "end_time": $("#End_time").val(),
                "price": $("#Price").val(),
                "number": $("#Number").val(),
                "contacts": $("#Contacts").val(),
                "workplace": $("#workplace").val(),
                "car_subsidy": $("#Car_subsidy").val(),
                "other_subsidy": $("#Other_subsidy").val(),
                "phone": $("#Phone").val(),
                "enterprise_id": $('select[name="Manag"]').val()
            }

            if (!thiz._isLegal2(obj)) {
                return false;
            } else {
                var param;

                if (thiz.isEdit) {
                    param = {
                        "id": thiz.taskId,
                        manage_id: thiz.manageId
                    }
                    url = '/task/updateTask/';
                } else {
                    param = {
                        manage_id: thiz.manageId
                    };
                    url = "/task/addTask";
                }
                $.extend(param, obj);
                start.postAjaxReq({
                    param: param,
                    type: "POST",
                    url: url,
                    success: function(data) {
                        alert(data.errmsg);
                        setTimeout(function() {
                            location.href = 'manager-list.html';
                        });
                    },
                    errorCb: function(data) {
                        alert(data.errmsg);
                    }
                });
            }

        });
    },
    //完善资料初始化数据
    initData: function(flag) {
        var thiz = this;
        if (flag) {
            $("#J-company").show();
            start.postAjaxReq({
                param: {
                    "manage_id": thiz.manageId,
                },
                type: "GET",
                url: "/worker/CompanyId",
                success: function(data) {
                    var obj = data.result.data;
                    var name = '';
                    for (var i = 0; i < obj.length; i++) {
                        name += '<option value="' + obj[i].c_id + '"  class="drop">' + obj[i].c_name + '</option>';
                    }
                    $("#Elect").append(name);
                }
            })
        } else {
            $("#J-address").show();
        }
    },

    _isLegal: function(obj, flag) {
        if (!$.trim(obj.name)) {
            alert("请填写姓名!");
            return false;
        }
        if (!$.trim(obj.idcar) || isNaN(obj.idcar)) {
            alert("请填写身份证号码");
            return false;
        }
        if (!$.trim(obj.phone) || isNaN(obj.phone)) {
            alert("请填写手机号码!");
            return false;
        }
        if (!$.trim(obj.verification) || isNaN(obj.verification)) {
            alert("请填写验证码!");
            return false;
        }
        if (flag) {
            if (obj.company == '0') {
                alert('请选择公司!');
                return false;
            }
        }
        var consent = $('input[name="Protocol"]').filter(':checked').val();
        if (consent == null) {
            alert("请选择同意协议!");
            return false;
        }
        return true;
    },
    bindEvent: function(flag) {
        var thiz = this;
        $("#submit").click(function() {
            var obj = {
                "name": $("#Name").val(),
                "idcar": $("#IDcar").val(),
                "sex": $('input[name="man"]').filter(':checked').val(),
                "phone": $("#number").val(),
                "verification": $("#Auth").val()
            }
            if (flag) {
                $.extend(obj, { "company_id": $('select[name="Elect"]').val() })
            } else {
                $.extend(obj, { "address": $("#Add").val() })
            }

            if (!thiz._isLegal(obj, flag)) {
                return false;
            } else {
                var param = {
                    "id": thiz.manageId
                }
                $.extend(param, obj);
                start.postAjaxReq({
                    param: param,
                    type: "POST",
                    url: "/worker/updateWorkerInfo",
                    success: function(data) {

                        alert(data.errmsg);
                       /* setTimeout(function() {
                            if (thiz.userType == 1) {
                                location.href = 'worker.html';
                            } else {
                                location.href = 'manager-list.html';
                            }
                        }, 2000)*/

                    },
                    errorCb: function(data) {
                        alert(data.errmsg);
                    }
                });
            }

        })
    },
    sendCode: function() {
        var wait = 60; //设置时间
        function time(o) {
            if (wait == 0) {
                o.removeAttribute("disabled");
                o.value = "发送短信";
                wait = 60;
            } else {
                o.setAttribute("disabled", true);
                o.value = "" + wait + "秒后重新发送";
                wait--;
                setTimeout(function() {
                    time(o)
                }, 1000);
            }
        }

        document.getElementById("send").onclick = function() {
            var phone = $("#number").val();
            if (phone == "" || isNaN(phone) || phone == null) {
                alert("请填写手机号码!");
            } else {
                time(this);
                start.postAjaxReq({
                    param: {
                        "mobile": phone,
                        "type": "register"
                    },
                    type: "GET",
                    url: "/common/verify",
                    success: function(data) {
                        alert(data.errmsg);
                    },
                    errorCb: function(data) {
                        alert(data.errmsg)
                    }
                });
            }

        }
    }
};
